package com.example.skillcinema.ui.screens.main

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.example.skillcinema.R
import com.example.skillcinema.databinding.FragmentLoadingBinding
import com.example.skillcinema.db.apis.RetrofitService
import com.example.skillcinema.ui.screens.main.viewmodels.MainViewModel
import kotlinx.coroutines.launch


class LoadingFragment : Fragment() {
    private var _binding: FragmentLoadingBinding? = null
    private val binding: FragmentLoadingBinding
        get() = _binding!!

    private val viewModel: MainViewModel by activityViewModels<MainViewModel>()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentLoadingBinding.inflate(inflater, container, false)
        lifecycleScope.launch {
            viewModel.isDataLoadFlow.collect {
                if (it)
                    findNavController().navigate(R.id.action_loadingFragment_to_homeFragment)
            }
        }
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}